package com.tanhua.dubbo.api.mongo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.domain.mongo.Sound;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@Service
public class SoundApiImpl implements SoundApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加数据到mongoDB
     * @param sound
     */
    @Override
    public String addSound(Sound sound) {
        ObjectId id = new ObjectId();
        sound.setId(id);
        mongoTemplate.insert(sound);
        return id.toHexString();

    }

    /**
     * 查询mongo中是否存在
     * @param loginUserId
     * @return
     */
    @Override
    public Sound findSoundByUserId(Long loginUserId) {
        Query query = new Query(Criteria.where("userId").is(loginUserId+""));
        return mongoTemplate.findOne(query,Sound.class);
    }

    /**
     * 修改数据
     * @param sound
     */
    @Override
    public void updateSound(Sound sound) {
        Query query = new Query(Criteria.where("userId").is(sound.getUserId()));
        Update update = new Update();
        update.set("soundUrl",sound.getSoundUrl());
        mongoTemplate.updateFirst(query,update,Sound.class);
    }
}
